﻿namespace System.ServiceModel.DomainServices
{
    using System;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.CompilerServices;

    internal static class TypeUtility
    {
        private static string[] systemAssemblyPublicKeyTokens = new string[] { "b77a5c561934e089", "31bf3856ad364e35", "b03f5f7f11d50a3a", "7cec85d7bea7798e" };

        internal static bool IsSystemAssembly(this Assembly assembly)
        {
            return IsSystemAssembly(assembly.FullName);
        }

        internal static bool IsSystemAssembly(this AssemblyName assemblyName)
        {
            return IsSystemAssembly(assemblyName.FullName);
        }

        internal static bool IsSystemAssembly(string assemblyFullName)
        {
            int index = assemblyFullName.IndexOf("PublicKeyToken=", StringComparison.OrdinalIgnoreCase);
            if (index == 0)
            {
                return false;
            }
            string publicKeyToken = assemblyFullName.Substring(index + 15);
            return systemAssemblyPublicKeyTokens.Any<string>(p => p.Equals(publicKeyToken, StringComparison.OrdinalIgnoreCase));
        }
    }
}

